﻿using GameFramework;
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TowerDefence_Zombie.Data;
using UnityEngine;
using UnityEngine.UI;

namespace TowerDefence_Zombie
{
    public class UISiginFrom : UGuiFormEx
    {
        [Title("签到奖励")] public List<ItemSigin> ItemSigins;

        [SerializeField, Title("签到按钮")] public Button SignInBtn;

        [Title("关闭按钮")] public Button CloseBtn;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            SignInBtn.onClick.AddListener(SignIn);
            CloseBtn.onClick.AddListener(CloseForm);
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            int signCount = GameEntry.Data.GetData<DataPlayer>().GetSiginCount();
            List<SiginRewardData> siginRewardData = GameEntry.Data.GetData<SystemData>().GetSiginRewardList();
            for (int i = 0; i < 6; i++)
            {
                ItemSigins[i].SetSignInfo(siginRewardData[i]);
                if (i + 1 <= signCount)
                    ItemSigins[i].SetSigned();
            }

            if (signCount >= 7)
                ItemSigins[6].SetSigned();

            InitSiginRewardBtnState();
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
            if (GameEntry.Data.GetData<DataPlayer>().CheckIsCanSigin() && SignInBtn.interactable == false)
                SignInBtn.interactable = true;
        }

        public void InitSiginRewardBtnState()
        {
            if (GameEntry.Data.GetData<DataPlayer>().CheckIsCanSigin())
                SignInBtn.interactable = true;
            else
                SignInBtn.interactable = false;
        }

        public void SignIn()
        {
            int signDay = GameEntry.Data.GetData<DataPlayer>().GetSiginCount() + 1;
            GameEntry.Data.GetData<DataPlayer>().SetSigin();
            ItemSigins[signDay - 1].SetSigned();
            SiginRewardData siginRewardData = GameEntry.Data.GetData<SystemData>().GetSiginRewardList()[signDay - 1];
            GameEntry.Data.GetData<DataPlayer>().AddCurrency(siginRewardData.MoneyId, siginRewardData.MoneyNum, CurrencyEvent.Sigin);
            SignInBtn.interactable = false;
        }

        protected override void OnClose(bool isShutdown, object userData)
        {
            base.OnClose(isShutdown, userData);
        }
        private void CloseForm()
        {
            Close();
        }
    }
}
